
package com.hp.ecs.sonar.util;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class IoUtil {

	public static final String PATH_PLUGINS = ".metadata/.plugins/";

	public static final String PATH_RUNTIME = "org.eclipse.core.runtime/.settings/";

	public static File createNewFile(String filename) {
		filename = filename.replace('\\', '/').trim();
		new File(filename.substring(0, filename.lastIndexOf('/'))).mkdirs();
		return new File(filename);
	}

	public static void write(Map<String, String> map, String file) {
		write(toString(map).getBytes(), file);
	}

	public static File getFile(File dir, String name) {
		return new File(dir.getAbsolutePath() + '/' + name);
	}

	public static Map<String, String> getProperties(InputStream in) {
		Map<String, String> map = new TreeMap<String, String>();
		byte[] bytes = read(in);
		String[] lines = new String(bytes).split("\n");
		for (String line : lines) {
			String s = line.trim();
			if (s.length() == 0 || s.charAt(0) == '#') {
				continue;
			}
			int index = s.indexOf('=');
			if (index == -1) {
				map.put(s, null);
			} else {
				map.put(s.substring(0, index).trim(), s.substring(index + 1).trim());
			}
		}
		return map;
	}

	public static void write(byte[] bytes, String filename) {
		FileOutputStream outStream = null;
		File f = new File(filename);
		try {
			if (!f.exists()) {
				f.createNewFile();
			}
			outStream = new FileOutputStream(f);
			outStream.write(bytes);
		} catch (Throwable e) {
			throw new RuntimeException(e);
		} finally {
			close(outStream);
		}
	}

	public static void deleteFiles(String dir) {
		File[] files = new File(dir).listFiles();
		for (int i = 0; i < files.length; i++)
			if (files[i].isFile())
				files[i].delete();
	}

	public static Map<? extends String, ? extends String> getResProps(String res) {
		return IoUtil.getProperties(IoUtil.class.getClassLoader().getResourceAsStream(res));
	}

	private static String toString(Map<String, String> map) {
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> me : map.entrySet()) {
			sb.append(me.getKey());
			if (me.getValue() != null) {
				sb.append('=').append(me.getValue());
			}
			sb.append("\n");
		}
		return sb.toString();
	}

	public static byte[] read(InputStream in) {
		List list = new LinkedList();
		int size = 0;
		try {
			int len;
			for (byte[] bytes = new byte[1024]; (len = in.read(bytes)) > 0;) {
				size += len;
				list.add(trim(bytes, len));
			}
			ByteBuffer buffer = ByteBuffer.allocate(size);
			for (Iterator it = list.iterator(); it.hasNext();) {
				buffer.put((byte[])it.next());
			}
			return buffer.array();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			close(in);
		}
	}

	private static byte[] trim(byte[] src, int len) {
		byte[] dest = new byte[len];
		for (int i = 0; i < len; i++) {
			dest[i] = src[i];
		}
		return dest;
	}

	public static void close(Closeable res) {
		if (res != null) {
			try {
				res.close();
			} catch (Exception localException1) {
				// IGNORE
			}
		}
	}

	public static byte[] read(File file) {
		FileInputStream in = null;
		List list = new LinkedList();

		int size = 0;
		try {
			in = new FileInputStream(file);
			int len;
			for (byte[] bytes = new byte[1024]; (len = in.read(bytes)) > 0;) {
				size += len;
				list.add(trim(bytes, len));
			}
			ByteBuffer buffer = ByteBuffer.allocate(size);
			for (Iterator it = list.iterator(); it.hasNext();) {
				buffer.put((byte[])it.next());
			}
			return buffer.array();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			close(in);
		}
	}

}
